#pragma strict
import System.Collections.Generic;
/////////////////////////////////////////////////////////////////////////////
//控制渲染.
/////////////////////////////////////////////////////////////////////////////
var alpha:float; //控制 所有材质的alpha.

var matList:List.<Material> = new List.<Material>();
var rs:Renderer[];

private var lastAlpha:float;

function Start () {
	rs = GetComponentsInChildren.<Renderer>();
	for(var r:Renderer in rs){
		matList.AddRange(r.materials);
	}
	ChangeAlpha(alpha);
}

function Update () {
	alpha = Mathf.Clamp01(alpha);
	if(lastAlpha != alpha){
		lastAlpha = alpha;
		ChangeAlpha(alpha);
	}
}

function ChangeAlpha(a:float){
	for(var mat:Material in matList){
		//忽略透明材质.
		if(mat.shader.renderQueue >= 3000)
			continue;
		if(mat.HasProperty("_Color"))
			mat.color.a = a;
	}
}

function OnDestroy(){
	matList.Clear();
	rs = null;
}